layui.define(['toastr'], function(exports) {
	var $ = layui.jquery;

	get_push();

	function get_push() {
		var d = new Date().getTime();
		$.ajax({
			type : "POST",
			url : push_server + '&' + d,
			dataType : "json",
			success : function(ret) {
				if (ret.status) {
					$msg = $.parseJSON(ret.data.data);
					push_info($msg);
					get_push();
				}
			}
		});
	}

	function push_info($msg) {
		var position;
		if ($msg.action.length) {
			$title = '<h3>[' + $msg.type + '] [' + $msg.action + ']</h3>';
		} else {
			$title = '<h3>[' + $msg.type + ']</h3>';
		}
		$content = '<p class="push-title">' + $msg.title + '</p>' + $msg.content;

		if (is_mobile()) {
			position = "toast-top-full-width";
		} else {
			position = "toast-bottom-right";
		}
		toastr.options = {
			"closeButton" : true,
			"positionClass" : position,
			"timeOut" : ws_push_time * 1000000
		};
		toastr.info($content, $title);
	}

	// 连接服务端
	//var socket = io('http://' + document.domain + ':2120');
	// 连接后登录
	//socket.on('connect', function() {
	//	socket.emit('login', '{:get_user_id()}');
	//});

	// 后端推送来消息时
	//socket.on('new_msg', function(msg) {
	//	get_push();
	//});

	window.push_info = push_info;
	window.get_push = get_push;
	exports('push', {});
});

